package cn.ito.zjgsu.web.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.ito.zjgsu.domain.UserLogin;
import cn.ito.zjgsu.domain.wrapper.DepartmentWrapper;
import cn.ito.zjgsu.service.DepartmentService;
import cn.ito.zjgsu.service.UserService;

@Controller
public class IndexController {

	@Resource
	private UserService userService;
	@Resource
	private DepartmentService departmentService;

	@RequestMapping(value = "/")
	public String index() {
		Subject currentUser = SecurityUtils.getSubject();

		if (currentUser.isAuthenticated()) {
			return "user/index";
		} else {
			return "login";
		}
	}

	@RequestMapping(value = "/login", method = RequestMethod.POST)
	public String Login(@RequestParam String username, @RequestParam String password, HttpServletRequest request,
			ModelMap map) {
		Subject currentUser = SecurityUtils.getSubject();
		UsernamePasswordToken token = new UsernamePasswordToken(username, password);
		String authticationError = null;
		try {
			currentUser.login(token);
		} catch (UnknownAccountException e) {
			authticationError = "用户名错误";
		} catch (IncorrectCredentialsException e) {
			authticationError = "密码错误";
		}

		map.addAttribute("error", authticationError);

		return "myindex2";

	}

	@RequestMapping(value = "/insertusertest", method = RequestMethod.GET)
	@ResponseBody
	public void insertUser() {
		UserLogin user = new UserLogin();
		user.setJobId(123);
		user.setNickname("jack");
		user.setPersonId(123124);
		user.setRoleId(3);
		user.setUsername("dhas");
		user.setPassword("111111");

		String status = userService.insertUser(user);

		System.out.println("insertStatus = " + status);
	}

	@RequestMapping(value = "/loginValidate", method = RequestMethod.GET)
	public String submitLoginForm(HttpServletRequest request, Model model) {
		String errorClassName = (String) request.getAttribute("shiroLoginFailure");
		String authticationError = null;
		if (UnknownAccountException.class.getName().equals(errorClassName)) {
			authticationError = "用户名/密码错误";
		} else if (IncorrectCredentialsException.class.getName().equals(errorClassName)) {
			authticationError = "用户名/密码错误";
		} else if (errorClassName != null) {
			authticationError = "未知错误：" + errorClassName;
		}
		model.addAttribute("authticationError", authticationError);
		return "myindex2";
	}

	@RequestMapping(value = "/testDepartment", method = RequestMethod.GET)
	@ResponseBody
	public void testDepartment() {
		List<DepartmentWrapper> ldw = departmentService.initDepartmentTree();
		//System.out.println(ldw.toString());
		System.out.println(DepartmentWrapper.toJsonArrayString(ldw));
	}
}
